package com.yeyks.account.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.account.dal.dao.SysRoleMapper;
import com.yeyks.account.dal.domain.SysRole;
import com.yeyks.account.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysDeptService sysDeptService;

//    @Override
    //@DataFilter(subDept = true, user = false)
//    public PageUtils queryPage(Map<String, Object> params) {
//        String roleName = (String)params.get("roleName");
//
//        IPage<SysRole> page = this.page(
//                new Query<SysRole>().getPage(params),
//                new QueryWrapper<SysRole>()
//                        .like(StringUtils.isNotBlank(roleName),"role_name", roleName)
//                //	.apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
//        );
//
//        for(SysRole SysRole : page.getRecords()){
//            SysDept SysDept = sysDeptService.getById(SysRole.getDeptId());
//            if(SysDept != null){
//                SysRole.setDeptName(SysDept.getName());
//            }
//        }
//
//        return new PageUtils(page);
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRole role) {
        role.setCreateTime(new Date());
        this.save(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRole role) {
        this.updateById(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleDeptService.saveOrUpdate(role.getRoleId(), role.getDeptIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与部门关联
        sysRoleDeptService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }

    @Override
    public List<SysRole> querySelfRoles(Long deptId,Long roleId)
    {
        return baseMapper.querySelfRoles(deptId,roleId);
    }


}
